<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\api\service\Token;
use app\lib\enum\MemberLogEnum;
use app\lib\enum\PageEnum;

/**
 * 积分详情
 * Class MemberLog
 * @package app\api\model
 */
class MemberLog extends BaseModel
{
    protected $autoWriteTimestamp = true;

    /**
     * @Desc 格式化时间
     *
     * @param $value
     *
     * @return false|string
     */
    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i', $value);
    }

    /**
     * @Desc 积分获取/消费详情
     *
     * @param $value
     * @param $data
     *
     * @return array
     */
    protected function getContentAttr($value, $data)
    {
        $content = [];
        if (MemberLogEnum::REDUCE == $data['type']) {
            switch ($data['get_from']) {
                case MemberLogEnum::FROM_REBACK:
                    $content['title'] = '订单退款';
                    break;
            }
            $content['value'] = "-{$data['number']}";
        } elseif (MemberLogEnum::INCREASE == $data['type']) {
            switch ($data['get_from']) {
                case MemberLogEnum::FROM_CONSUMPTION:
                    $content['title'] = '订单支付';
                    break;
                case MemberLogEnum::FROM_SIGN:
                    $content['title'] = '签到奖励';
                    break;
                case MemberLogEnum::FROM_ACTIVITY:
                    $content['title'] = '活动奖励';
                    break;
                case MemberLogEnum::FROM_CANCEL_REBACK:
                    $content['title'] = '取消退款';
                    break;
            }
            $content['value'] = "+{$data['number']}";
        }
        return $content;
    }

    /**
     * @Desc 查询积分详情
     *
     * @param $page
     *
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public static function getMemberDetailList($page)
    {
        // 分页数量
        $listRows = PageEnum::MEMBER;
        $id = Token::getUidFromCache();
        $member = (new self)->field('number, type, get_from, create_time')->where('user_weixin_id', '=', $id)->page($page, $listRows)->order('create_time desc')->select();
        return $member->append(['content'])->toArray();
    }

    /**
     * @Desc 添加积分记录
     * @param $param
     *
     * @return MemberLog
     */
    public static function insertMemberData($param)
    {
        return self::create($param);
    }
}